package com.sharemarking.wa.platform.service;

import com.sharemarking.wa.common.Page;
import com.sharemarking.wa.common.ResponseParams;
import com.sharemarking.wa.common.exception.RequestException;
import com.sharemarking.wa.platform.dto.monthlyStatement.MonthListPageDto;
import com.sharemarking.wa.platform.dto.monthlyStatement.MonthlyStatementListDto;
import com.sharemarking.wa.platform.dto.monthlyStatement.StatementListDto;
import com.sharemarking.wa.platform.mapper.MonthlyStatementMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dxy
 */
@Service
public class MonthlyStatementService {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private SecurityService ss;
    @Autowired
    private MonthlyStatementMapper monthlyStatementMapper;


    public ResponseParams<?> getTotalList() {
        Integer memberId = ss.getCurrentUserId();

        int pageIndex = Integer.parseInt(StringUtils.isEmpty(request.getParameter("pageIndex")) ? "1" : request.getParameter("pageIndex"));
        int pageSize = Integer.parseInt(StringUtils.isEmpty(request.getParameter("pageSize")) ? "10" : request.getParameter("pageSize"));

        Map<String, Object> params = new HashMap<>();
        params.put("pageIndex", (pageIndex - 1) * pageSize);
        params.put("pageSize", pageSize);
        params.put("memberId", memberId);

        List<MonthlyStatementListDto> list = monthlyStatementMapper.getList(params);

        return ResponseParams.ok(new Page<>(list, pageIndex, pageSize, monthlyStatementMapper.getListTotal(params), list.size() == pageSize));
    }


    public ResponseParams<?> getMonthList() throws Exception{
        Integer memberId = ss.getCurrentUserId();
        String time = request.getParameter("time");

        int pageIndex = Integer.parseInt(StringUtils.isEmpty(request.getParameter("pageIndex")) ? "1" : request.getParameter("pageIndex"));
        int pageSize = Integer.parseInt(StringUtils.isEmpty(request.getParameter("pageSize")) ? "10" : request.getParameter("pageSize"));

        Map<String, Object> params = new HashMap<>();
        params.put("pageIndex", (pageIndex - 1) * pageSize);
        params.put("pageSize", pageSize);
        params.put("memberId", memberId);

        Date date;
        try {
            SimpleDateFormat s = new SimpleDateFormat("yyyy年MM月");
            date = s.parse(time);
        }catch (Exception e){
            throw RequestException.create(ResponseParams.isDataError("时间格式错误"));
        }
        params.put("time", date);
        List<StatementListDto> list = monthlyStatementMapper.getMonthList(params);


        return ResponseParams.ok(new Page<>(list, pageIndex, pageSize, monthlyStatementMapper.getMonthListTotal(params), list.size() == pageSize));
    }


    public ResponseParams<?> getPCMonthList() throws Exception{
        Integer memberId = ss.getCurrentUserId();


        int pageIndex = Integer.parseInt(StringUtils.isEmpty(request.getParameter("pageIndex")) ? "1" : request.getParameter("pageIndex"));
        int pageSize = Integer.parseInt(StringUtils.isEmpty(request.getParameter("pageSize")) ? "10" : request.getParameter("pageSize"));

        Map<String, Object> params = new HashMap<>();
        params.put("pageIndex", (pageIndex - 1) * pageSize);
        params.put("pageSize", pageSize);
        params.put("memberId", memberId);

        List<StatementListDto> list = monthlyStatementMapper.getPCMonthList(params);

        return ResponseParams.ok(new MonthListPageDto(list, monthlyStatementMapper.isMonthlyPay(memberId),pageIndex, pageSize, monthlyStatementMapper.getPCMonthListTotal(params), list.size() == pageSize));
    }

}
